<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>

</style>
<script type="text/javascript">
    var sqlExcelQueryExpressionDataGrid;
    var sqlExcelColumnDatagrid;
    var dialog;
    $(function() {
        sqlExcelQueryExpressionDataGrid = $('#sqlExcelQueryExpressionDataGrid').datagrid({
            url : '${path}/sqlExcelQueryExpression/dataGrid?fid=${id}',
            striped : true,
            rownumbers : true,
            singleSelect : true,
            idField : 'id',
            columns : [ [
                {
                width : '100',
                title : '外键',
                field : 'fId'
                ,hidden : true
            },
                {
                width : '100',
                title : 'id',
                field : 'id'
                ,hidden : true
            }, {
                width : '100',
                title : '名称',
                field : 'name'
            },
            {
                width : '100',
                title : '类型',
                field : 'type',
                formatter:function (value,row,index) {
                    if(value=='String'){
                        return "字符串"
                    }
                    if(value=='Date'){
                        return "日期"
                    }
                    if(value=='DownSelect'){
                        return "下拉选择"
                    }
                    if(value=='defineSQL'){
                        return "自定义SQL选择"
                    }
                    if(value=='selectUser'){
                        return "选人"
                    }
                    if(value=='selectOrg'){
                        return "选部门"
                    }
                    if(value=='selectPosition'){
                        return "选职位"
                    }
                }
            },
            {
                width : '100',
                title : '参数字段',
                field : 'key'
            },
            {
                width : '100',
                title : '占位符',
                field : 'placeholder'
            },
                {
                width : '300',
                title : '表达式',
                field : 'expression'
            }, {
                width : '300',
                title : '自定义sql',
                field : 'querySql'
            }, {
                width : '100',
                title : '顺序',
                field : 'seq'
            },
               
            ]],
            frozenColumns : [ [{
                field : 'action',
                title : '操作',
                width : 150,
                formatter : function(value, row, index) {
                    var str = '';
                    str += $.formatString('<a href="javascript:void(0)" class="sqlExcelQueryExpression-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editSqlExcelQueryExpressionFun(\'{0}\');" >编辑</a>', row.id);
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="sqlExcelQueryExpression-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deleteSqlExcelQueryExpressionFun(\'{0}\');" >删除</a>', row.id);
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.sqlExcelQueryExpression-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.sqlExcelQueryExpression-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#sqlExcelQueryExpressionToolbar'
        });

        sqlExcelColumnDatagrid=$('#sqlExcelColumn').datagrid({
            url : '${path}/sqlExcelField/dataGrid?fid=${id}',
            striped : true,
            rownumbers : true,
            singleSelect : true,
            idField : 'id',
            columns : [ [
                {
                    width : '100',
                    title : '外键',
                    field : 'fid'
                    ,hidden : true
                },
                {
                    width : '100',
                    title : 'id',
                    field : 'id'
                    ,hidden : true
                }, {
                    width : '100',
                    title : '字段名称',
                    field : 'keyName'
                },
                {
                    width : '100',
                    title : '列名',
                    field : 'columnName'
                },
                {
                    width : '100',
                    title : '显示类型',
                    field : 'columnType',
                    formatter:function (value,row,index) {
                        if(value=='String'){
                            return "字符串"
                        }
                        if(value=='Date'){
                            return "日期"
                        }
                        if(value=='DateTime'){
                            return "日期时间"
                        }
                    }
                },
                {
                    width : '100',
                    title : '排序',
                    field : 'seq'
                }
            ]],
            frozenColumns : [ [{
                field : 'action',
                title : '操作',
                width : 150,
                formatter : function(value, row, index) {
                    var str = '';
                    str += $.formatString('<a href="javascript:void(0)" class="sqlExcelColumn-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editsqlExcelColumnFun(\'{0}\');" >编辑</a>', row.id);
                    str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                    str += $.formatString('<a href="javascript:void(0)" class="sqlExcelColumn-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red\'" onclick="deletesqlExcelColumnFun(\'{0}\');" >删除</a>', row.id);
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.sqlExcelColumn-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.sqlExcelColumn-easyui-linkbutton-del').linkbutton({text:'删除'});
            },
            toolbar : '#sqlExcelColumnToolbar'
        })
    });

    function addSqlExcelQueryExpressionFun() {
        parent.$.modalDialog({
            title : '添加',
            width : 500,
            height : 350,
            href : '${path}/sqlExcelQueryExpression/addPage?fId=${id}',
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = sqlExcelQueryExpressionDataGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#sqlExcelQueryExpressionAddForm');
                    f.submit();
                }
            } ]
        });
    }

    function editSqlExcelQueryExpressionFun(id) {
        if (id == undefined) {
            var rows = sqlExcelQueryExpressionDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
            sqlExcelQueryExpressionDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.modalDialog({
            title : '编辑',
            width : 500,
            height : 350,
            href : '${path}/sqlExcelQueryExpression/editPage/' + id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = sqlExcelQueryExpressionDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#sqlExcelQueryExpressionEditForm');
                    f.submit();
                }
            } ]
        });
    }

    function deleteSqlExcelQueryExpressionFun(id) {
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = sqlExcelQueryExpressionDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            sqlExcelQueryExpressionDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/sqlExcelQueryExpression/delete/'+id, {}
                , function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        sqlExcelQueryExpressionDataGrid.datagrid('reload');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }

    function addsqlExcelColumnFun() {
         dialog=$('<div>').dialog({
            title : '新增',
            width : 500,
            height : 300,
            modal:true,
            onClose:function(){
                $(this).dialog('destroy');
            },
            href : '${path}/sqlExcelField/addPage?fid='+${sqlExcelReport.id},
            buttons : [ {
                text : '确定',
                handler : function() {
                    $('#sqlExcelFieldAddForm').submit();
                }
            } ]
        })
    }

    function editsqlExcelColumnFun(id){
        dialog=$('<div>').dialog({
            title : '编辑',
            width : 500,
            height : 300,
            modal:true,
            onClose:function(){
                $(this).dialog('destroy');
            },
            href : '${path}/sqlExcelField/editPage/'+id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    $('#sqlExcelFieldEditForm').submit();
                }
            } ]
        })
    }

    function deletesqlExcelColumnFun(id){
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = sqlExcelColumnDatagrid.datagrid('getSelections');
            id = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            sqlExcelColumnDatagrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/sqlExcelField/delete/'+id, {}
                    , function(result) {
                        if (result.success) {
                            parent.$.messager.alert('提示', result.msg, 'info');
                            sqlExcelColumnDatagrid.datagrid('reload');
                        }
                        progressClose();
                    }, 'JSON');
            }
        });
    }

    function saveSqlExcelSql() {
        var sql=$('#querySql').val();
        $.ajax({
            url:'${path}/sqlExcelReport/updateSqlExcelQuerySql',
            data:{"id":"${sqlExcelReport.id}","querySql":sql},
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.success){
                    showMsg(data.msg);
                }else{
                    $.messager.alert('错误',data.msg,'error');
                }
            }
        })
    }
</script>
<div class="easyui-layout" data-options="fit:true,border:false" style="overflow-y: hidden">
 <div data-options="region:'north',border:false,fit:false" style=" overflow: hidden;background-color: #fff">
 	<h3 style="text-align: center;">报表名称：${sqlExcelReport.name}</h3>
 	 <h3 style="padding:15px;padding-bottom:0px;padding-top:0px;">查询sql</h3>
 	<textarea name="querySql" style="min-height:100px;margin:15px;margin-top:0px;width:90%;float: left;" class="easyui-text" id="querySql">${sqlExcelReport.sql}</textarea>
 	<a class="easyui-linkbutton" onclick="saveSqlExcelSql()">保存</a>
     <div style="clear: both;color:#999;padding:10px 15px;padding-top: 0px;">
         例子：select * from sys_user where 1=1 #1 #2 order by create_time desc，其中#1和#2是占位符，对应查询参数
     </div>
 </div>
   <div data-options="region:'center',border:true,title:'设置查询参数'">
        <table id="sqlExcelQueryExpressionDataGrid" data-options="border:false" style=""></table>
   </div>
       <div data-options="region:'south',border:true,title:'显示列'" style="height: 400px;">
        <table class="grid" id="sqlExcelColumn">
        </table>
    </div>
</div>
<div id="sqlExcelQueryExpressionToolbar" style="display: none;">
    <a onclick="addSqlExcelQueryExpressionFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
</div>
<div id="sqlExcelColumnToolbar" style="display: none;">
    <a onclick="addsqlExcelColumnFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
</div>
